{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Rendering Images inside a Pandas DataFrame.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "authorship_tag": "ABX9TyNSGvUVO3CMtDVfOoG/7lyV",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Rendering_Images_inside_a_Pandas_DataFrame.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SGzj9_pmsS-y",
        "colab_type": "text"
      },
      "source": [
        "# **Rendering Images inside a Pandas DataFrame**\n",
        "\n",
        "## **Ever thought about rendering images inside a dataframe? I’m glad you did!**\n",
        "\n",
        "\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1400/1*Kewz0xLvwocuc8INzphzmA.jpeg)\n",
        "\n",
        "\n",
        "I can see the ***question mark (?)*** on most of your faces. Because even I had one before discovering that we can also render images inside a dataframe. And believe me, it’s simple and easy, all you need to know is to ***create a dataframe and I’ll pick you up from there***.\n",
        "\n",
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Os0Ok4kZsj9J",
        "colab_type": "text"
      },
      "source": [
        "# **Steps to be followed**\n",
        "\n",
        "\n",
        "1.   Import two necessary libraries such as `pandas` and `HTML`.\n",
        "\n",
        "2.  Create a pandas dataframe of your choice and store it in the variable `df`.\n",
        "\n",
        "3.  Create a list called `country` and then store all the paths of the images that you want to render.\n",
        "\n",
        "4.  Assign the `country` list to the existing dataframe `df`. This would be appended as a new column to the existing dataframe.\n",
        "\n",
        "5.  Write a function that will convert the given path of the images to the HTML tag.\n",
        "\n",
        "6.  Render the dataframe as an HTML table, and then call the HTML method to display the rendered images.\n",
        "\n",
        "---\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "n-9krkMys-Ou",
        "colab_type": "text"
      },
      "source": [
        "# **Bonus**\n",
        "\n",
        "![alt text](https://si.wsj.net/public/resources/images/BN-LJ639_1123hj_H_20151120160634.jpg)\n",
        "\n",
        "Also, I will show you guys “***Saving the dataframe as an HTML page***” more like a webpage.\n",
        "\n",
        "\n",
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aNV0RWYxtZ3s",
        "colab_type": "text"
      },
      "source": [
        "# **Code**\n",
        "\n",
        "Follow the above steps 1–6 to successfully render the images inside a dataframe. I will also be repeating the steps here too. You don’t have to scroll up to read every time.\n",
        "\n",
        "**Import two necessary libraries such as `pandas` and `HTML`.**\n",
        "\n",
        "\n",
        "The reason for importing `pandas` library is for creating and storing the data in a dataframe. Similarly, `HTML` library is used to render the image (HTML tags) inside a dataframe.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_6c-L-4etkb4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Importing the necessary libraries\n",
        "import pandas as pd\n",
        "from IPython.core.display import HTML"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kn7z4PKNtm7G",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "> **There is no need to manually install using `pip` command, just importing it will serve the purpose**\n",
        "\n",
        "---\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OjHh41WXtqpf",
        "colab_type": "text"
      },
      "source": [
        "**Create a pandas dataframe of your choice and store it in the variable `df`.**\n",
        "\n",
        "\n",
        "This step is straightforward because almost everyone knows how to create a dataframe. If not, then don’t forget to go through this [article here](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html). I will create the dataframe by taking the COVID19 data from [worldometers.info](https://www.worldometers.info/coronavirus/). Keep in mind this data changes every hour. I’ll be making five different columns such as `Total Cases`, `Total Deaths`, `Total Recovered`, `Total Tests`, `Population`. For the rows, I will choose `USA`, `Brazil`, `Russia`, `India`, `Peru`.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wkRKMszct_Yx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 202
        },
        "outputId": "8f207752-0ab7-4e38-b1d1-2b5b179043a7"
      },
      "source": [
        "# Create a dataframe using pandas library\n",
        "df = pd.DataFrame([[2768571, 130655, 1155027, 34713051, 331002277],        \n",
        "                   [1448753, 60632, 790040, 3070447, 212558178],\n",
        "                   [654405, 9536, 422931, 19852167, 145934619],\n",
        "                   [605216, 17848, 359891, 8826585, 1379974505],\n",
        "                   [288477, 9860, 178245, 1699369, 32969875]],\n",
        "                   columns = ['Total Cases', 'Total Deaths', 'Total Recovered', 'Total Tests', 'Population'])\n",
        "df"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Total Cases</th>\n",
              "      <th>Total Deaths</th>\n",
              "      <th>Total Recovered</th>\n",
              "      <th>Total Tests</th>\n",
              "      <th>Population</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2768571</td>\n",
              "      <td>130655</td>\n",
              "      <td>1155027</td>\n",
              "      <td>34713051</td>\n",
              "      <td>331002277</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1448753</td>\n",
              "      <td>60632</td>\n",
              "      <td>790040</td>\n",
              "      <td>3070447</td>\n",
              "      <td>212558178</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>654405</td>\n",
              "      <td>9536</td>\n",
              "      <td>422931</td>\n",
              "      <td>19852167</td>\n",
              "      <td>145934619</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>605216</td>\n",
              "      <td>17848</td>\n",
              "      <td>359891</td>\n",
              "      <td>8826585</td>\n",
              "      <td>1379974505</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>288477</td>\n",
              "      <td>9860</td>\n",
              "      <td>178245</td>\n",
              "      <td>1699369</td>\n",
              "      <td>32969875</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   Total Cases  Total Deaths  Total Recovered  Total Tests  Population\n",
              "0      2768571        130655          1155027     34713051   331002277\n",
              "1      1448753         60632           790040      3070447   212558178\n",
              "2       654405          9536           422931     19852167   145934619\n",
              "3       605216         17848           359891      8826585  1379974505\n",
              "4       288477          9860           178245      1699369    32969875"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CcdVQLzguDab",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HI4WupdJuD_E",
        "colab_type": "text"
      },
      "source": [
        "**Create a list called `country` and then store all the paths of the images (flags) that you want to render.**\n",
        "\n",
        "This step is obvious, for consistency we will create a list that has all the country flag paths of the countries listed above. The country flag source can be found [here](https://www.countries-ofthe-world.com/)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4IPyEebfuZXH",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 104
        },
        "outputId": "11960ac7-82df-43b5-9061-c89916d9a167"
      },
      "source": [
        "# Create a list named country to store all the image paths\n",
        "country = [\n",
        "'https://www.countries-ofthe-world.com/flags-normal/flag-of-United-States-of-America.png',\n",
        "'https://www.countries-ofthe-world.com/flags-normal/flag-of-Brazil.png',\n",
        "'https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png',\n",
        "'https://www.countries-ofthe-world.com/flags-normal/flag-of-India.png',\n",
        "'https://www.countries-ofthe-world.com/flags-normal/flag-of-Peru.png'\n",
        "]\n",
        "\n",
        "country"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['https://www.countries-ofthe-world.com/flags-normal/flag-of-United-States-of-America.png',\n",
              " 'https://www.countries-ofthe-world.com/flags-normal/flag-of-Brazil.png',\n",
              " 'https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png',\n",
              " 'https://www.countries-ofthe-world.com/flags-normal/flag-of-India.png',\n",
              " 'https://www.countries-ofthe-world.com/flags-normal/flag-of-Peru.png']"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VZv1YgC5ugsf",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YdN0zsp_uj8t",
        "colab_type": "text"
      },
      "source": [
        "**Assign the `country` list to the existing dataframe `df`. This would be appended as a new column to the existing dataframe.**\n",
        "\n",
        "\n",
        "This is one of the simplest steps because you need to ***literally*** assign the list named `country` to the dataframe’s new column named `Country`."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UhLpmLguute2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Assigning the new list as a new column of the dataframe\n",
        "df['Country'] = country"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yjv4Jk1AuvuD",
        "colab_type": "text"
      },
      "source": [
        "By doing so when you print the dataframe `df`. You will get the below as the result."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JzOyqquduzJ3",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 202
        },
        "outputId": "5206663c-e3c7-443b-b00d-f9e4e3f12b42"
      },
      "source": [
        "df"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Total Cases</th>\n",
              "      <th>Total Deaths</th>\n",
              "      <th>Total Recovered</th>\n",
              "      <th>Total Tests</th>\n",
              "      <th>Population</th>\n",
              "      <th>Country</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2768571</td>\n",
              "      <td>130655</td>\n",
              "      <td>1155027</td>\n",
              "      <td>34713051</td>\n",
              "      <td>331002277</td>\n",
              "      <td>https://www.countries-ofthe-world.com/flags-no...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1448753</td>\n",
              "      <td>60632</td>\n",
              "      <td>790040</td>\n",
              "      <td>3070447</td>\n",
              "      <td>212558178</td>\n",
              "      <td>https://www.countries-ofthe-world.com/flags-no...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>654405</td>\n",
              "      <td>9536</td>\n",
              "      <td>422931</td>\n",
              "      <td>19852167</td>\n",
              "      <td>145934619</td>\n",
              "      <td>https://www.countries-ofthe-world.com/flags-no...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>605216</td>\n",
              "      <td>17848</td>\n",
              "      <td>359891</td>\n",
              "      <td>8826585</td>\n",
              "      <td>1379974505</td>\n",
              "      <td>https://www.countries-ofthe-world.com/flags-no...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>288477</td>\n",
              "      <td>9860</td>\n",
              "      <td>178245</td>\n",
              "      <td>1699369</td>\n",
              "      <td>32969875</td>\n",
              "      <td>https://www.countries-ofthe-world.com/flags-no...</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "   Total Cases  ...                                            Country\n",
              "0      2768571  ...  https://www.countries-ofthe-world.com/flags-no...\n",
              "1      1448753  ...  https://www.countries-ofthe-world.com/flags-no...\n",
              "2       654405  ...  https://www.countries-ofthe-world.com/flags-no...\n",
              "3       605216  ...  https://www.countries-ofthe-world.com/flags-no...\n",
              "4       288477  ...  https://www.countries-ofthe-world.com/flags-no...\n",
              "\n",
              "[5 rows x 6 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qVnxZdTMu2nd",
        "colab_type": "text"
      },
      "source": [
        "**Write a function that will convert the given path of the images (links) to the HTML tag**\n",
        "\n",
        "Here you need to write a function that will convert the links to tags, as you saw in the previous result (Dataframe), the Country columns have all the links, so this step is necessary."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8uDwHrd4u6o7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Converting links to html tags\n",
        "def path_to_image_html(path):\n",
        "    return '<img src=\"'+ path + '\" width=\"60\" >'"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bIBKScwqu8JK",
        "colab_type": "text"
      },
      "source": [
        "The return type of the function is the traditional `img` tag, that we use to render images on a webpage. If you don’t know what I’m talking about see [this](https://www.w3schools.com/tags/tag_img.asp). The reason for doing this is that if all the paths are converted into image tags, then at the end with the help of a built-in HTML method all this will be rendered as images.\n",
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "E3stlOzcvCiU",
        "colab_type": "text"
      },
      "source": [
        "**Render the dataframe as an HTML table, and then call the HTML method to display the rendered images**\n",
        "\n",
        "\n",
        "We will use the `to_html` method to convert the dataframe into an HTML table. There are some parameters that we need to play with first the `escape = False`, escapes the HTML tags, we need to call the `path_to_image_html` method, convert it into a dictionary, and assign it to the formatters built-in variable. To know more about this method, please visit [here](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html)."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K7j4mFPzvOna",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 139
        },
        "outputId": "d8b19d4d-bb39-448b-e935-1eb9b971a087"
      },
      "source": [
        "# Rendering the dataframe as HTML table\n",
        "df.to_html(escape=False, formatters=dict(Country=path_to_image_html))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic": {
              "type": "string"
            },
            "text/plain": [
              "'<table border=\"1\" class=\"dataframe\">\\n  <thead>\\n    <tr style=\"text-align: right;\">\\n      <th></th>\\n      <th>Total Cases</th>\\n      <th>Total Deaths</th>\\n      <th>Total Recovered</th>\\n      <th>Total Tests</th>\\n      <th>Population</th>\\n      <th>Country</th>\\n    </tr>\\n  </thead>\\n  <tbody>\\n    <tr>\\n      <th>0</th>\\n      <td>2768571</td>\\n      <td>130655</td>\\n      <td>1155027</td>\\n      <td>34713051</td>\\n      <td>331002277</td>\\n      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-United-States-of-America.png\" width=\"60\" ></td>\\n    </tr>\\n    <tr>\\n      <th>1</th>\\n      <td>1448753</td>\\n      <td>60632</td>\\n      <td>790040</td>\\n      <td>3070447</td>\\n      <td>212558178</td>\\n      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Brazil.png\" width=\"60\" ></td>\\n    </tr>\\n    <tr>\\n      <th>2</th>\\n      <td>654405</td>\\n      <td>9536</td>\\n      <td>422931</td>\\n      <td>19852167</td>\\n      <td>145934619</td>\\n      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png\" width=\"60\" ></td>\\n    </tr>\\n    <tr>\\n      <th>3</th>\\n      <td>605216</td>\\n      <td>17848</td>\\n      <td>359891</td>\\n      <td>8826585</td>\\n      <td>1379974505</td>\\n      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-India.png\" width=\"60\" ></td>\\n    </tr>\\n    <tr>\\n      <th>4</th>\\n      <td>288477</td>\\n      <td>9860</td>\\n      <td>178245</td>\\n      <td>1699369</td>\\n      <td>32969875</td>\\n      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Peru.png\" width=\"60\" ></td>\\n    </tr>\\n  </tbody>\\n</table>'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GGfwn4UOvQVE",
        "colab_type": "text"
      },
      "source": [
        "By executing this you will get the result as an HTML table that looks familiar to you. I will provide only the first table row snapshot below:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NExm4mMbvRmR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "<table border=\"1\" class=\"dataframe\">\n",
        "  <thead>\n",
        "    <tr style=\"text-align: right;\">\n",
        "      <th></th>\n",
        "      <th>Total Cases</th>\n",
        "      <th>Total Deaths</th>\n",
        "      <th>Total Recovered</th>\n",
        "      <th>Total Tests</th>\n",
        "      <th>Population</th>\n",
        "      <th>Country</th>\n",
        "    </tr>\n",
        "  </thead>\n",
        "  <tbody>\n",
        "    <tr>\n",
        "      <th>0</th>\n",
        "      <td>2768571</td>\n",
        "      <td>130655</td>\n",
        "      <td>1155027</td>\n",
        "      <td>34713051</td>\n",
        "      <td>331002277</td>\n",
        "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-United-States-of-America.png\" width=\"60\" ></td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>1</th>\n",
        "      <td>1448753</td>\n",
        "      <td>60632</td>\n",
        "      <td>790040</td>\n",
        "      <td>3070447</td>\n",
        "      <td>212558178</td>\n",
        "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Brazil.png\" width=\"60\" ></td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>2</th>\n",
        "      <td>654405</td>\n",
        "      <td>9536</td>\n",
        "      <td>422931</td>\n",
        "      <td>19852167</td>\n",
        "      <td>145934619</td>\n",
        "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png\" width=\"60\" ></td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>3</th>\n",
        "      <td>605216</td>\n",
        "      <td>17848</td>\n",
        "      <td>359891</td>\n",
        "      <td>8826585</td>\n",
        "      <td>1379974505</td>\n",
        "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-India.png\" width=\"60\" ></td>\n",
        "    </tr>\n",
        "    <tr>\n",
        "      <th>4</th>\n",
        "      <td>288477</td>\n",
        "      <td>9860</td>\n",
        "      <td>178245</td>\n",
        "      <td>1699369</td>\n",
        "      <td>32969875</td>\n",
        "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Peru.png\" width=\"60\" ></td>\n",
        "    </tr>\n",
        "  </tbody>\n",
        "</table>"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4_zDZjxPvShI",
        "colab_type": "text"
      },
      "source": [
        "And the last thing to do is to call the built-in `HTML` method and pass the whole dataframe as the parameter."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-0-2FPT0vWoo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 312
        },
        "outputId": "44287581-ec09-4fae-af2a-d26dc6db2bf6"
      },
      "source": [
        "# Rendering the images in the dataframe using the HTML method.\n",
        "HTML(df.to_html(escape=False,formatters=dict(Country=path_to_image_html)))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Total Cases</th>\n",
              "      <th>Total Deaths</th>\n",
              "      <th>Total Recovered</th>\n",
              "      <th>Total Tests</th>\n",
              "      <th>Population</th>\n",
              "      <th>Country</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>2768571</td>\n",
              "      <td>130655</td>\n",
              "      <td>1155027</td>\n",
              "      <td>34713051</td>\n",
              "      <td>331002277</td>\n",
              "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-United-States-of-America.png\" width=\"60\" ></td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1448753</td>\n",
              "      <td>60632</td>\n",
              "      <td>790040</td>\n",
              "      <td>3070447</td>\n",
              "      <td>212558178</td>\n",
              "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Brazil.png\" width=\"60\" ></td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>654405</td>\n",
              "      <td>9536</td>\n",
              "      <td>422931</td>\n",
              "      <td>19852167</td>\n",
              "      <td>145934619</td>\n",
              "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png\" width=\"60\" ></td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>605216</td>\n",
              "      <td>17848</td>\n",
              "      <td>359891</td>\n",
              "      <td>8826585</td>\n",
              "      <td>1379974505</td>\n",
              "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-India.png\" width=\"60\" ></td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>288477</td>\n",
              "      <td>9860</td>\n",
              "      <td>178245</td>\n",
              "      <td>1699369</td>\n",
              "      <td>32969875</td>\n",
              "      <td><img src=\"https://www.countries-ofthe-world.com/flags-normal/flag-of-Peru.png\" width=\"60\" ></td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6Lzj7wtdvX_n",
        "colab_type": "text"
      },
      "source": [
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sm3npIREvYo-",
        "colab_type": "text"
      },
      "source": [
        "# **Bonus**\n",
        "\n",
        "As I promised, I will show you guys how to ***save the dataframe as an HTML page***. This would be often helpful when you want to send immense dataframes with images to your boss or whoever."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ylv1qGmxveCi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Saving the dataframe as a webpage\n",
        "df.to_html('webpage.html',escape=False, formatters=dict(Country=path_to_image_html))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_st9beThvfsQ",
        "colab_type": "text"
      },
      "source": [
        "As seen above you only need to provide the path to save the webpage, since I am using my Google Colab, it will be saved in my notebook environment, on opening it you will the below-shown result.\n",
        "\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1400/1*Q9peI8Gx9S8ewpfP6xx5Cw.png)\n",
        "\n",
        "\n",
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k4EQrpZ1vkKs",
        "colab_type": "text"
      },
      "source": [
        "# **Full Code**\n",
        "\n",
        "\n",
        "The full code of this tutorial can be found below:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "X0xF52BLvolP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Importing the necessary libraries\n",
        "import pandas as pd\n",
        "from IPython.core.display import HTML\n",
        "# Create a dataframe using pandas library\n",
        "df = pd.DataFrame([[2768571, 130655, 1155027, 34713051, 331002277],\n",
        "[1448753, 60632, 790040, 3070447, 212558178],[654405, 9536, 422931, 19852167, 145934619],[605216, 17848, 359891, 8826585, 1379974505],[288477, 9860, 178245, 1699369, 32969875]], columns = ['Total Cases', 'Total Deaths', 'Total Recovered', 'Total Tests', 'Population'])\n",
        "# Create a list named country to store all the image paths\n",
        "country = ['https://www.countries-ofthe-world.com/flags-normal/flag-of-United-States-of-America.png','https://www.countries-ofthe-world.com/flags-normal/flag-of-Brazil.png','https://www.countries-ofthe-world.com/flags-normal/flag-of-Russia.png','https://www.countries-ofthe-world.com/flags-normal/flag-of-India.png','https://www.countries-ofthe-world.com/flags-normal/flag-of-Peru.png']\n",
        "# Assigning the new list as a new column of the dataframe\n",
        "df['Country'] = country\n",
        "# Converting links to html tags\n",
        "def path_to_image_html(path):\n",
        "    return '<img src=\"'+ path + '\" width=\"60\" >'\n",
        "# Rendering the dataframe as HTML table\n",
        "df.to_html(escape=False, formatters=dict(Country=path_to_image_html))\n",
        "# Rendering the images in the dataframe using the HTML method.\n",
        "HTML(df.to_html(escape=False,formatters=dict(Country=path_to_image_html)))\n",
        "# Saving the dataframe as a webpage\n",
        "df.to_html('webpage.html',escape=False, formatters=dict(Country=path_to_image_html))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aeWdgn_evqt6",
        "colab_type": "text"
      },
      "source": [
        "The main inspiration for writing this article was because of this forum on [stack overflow](https://stackoverflow.com/questions/53468558/adding-image-to-pandas-dataframe). Special thanks to [Chitown88](https://stackoverflow.com/users/7752296/chitown88).\n",
        "\n",
        "\n",
        "\n",
        "Thank you guys that’s it for this tutorial, I hope you guys learned something new today. If you guys have any comments or concerns about the code, then use the comments section wisely. Stay tuned for more updates, until then goodbye. Stay safe and happy coding!\n",
        "\n",
        "---\n",
        "\n"
      ]
    }
  ]
}